package com.tian.controller.system;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tian.entity.UserSys;
import com.tian.service.UserSysService;
import com.tian.util.Constast;
import com.tian.util.DataGridView;
import com.tian.util.PinyinUtils;
import com.tian.util.ResultObj;
import com.tian.vo.UserSysVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author tianwc  公众号：java后端技术全栈、面试专栏
 * @version 1.0.0
 * @date 2023年04月22日 15:51
 * 博客地址：<a href="http://woaijava.cc/">博客地址</a>
 * <p>
 * 用户管理
 */
@RestController
@RequestMapping("/user/sys")
public class UserSysController {
    @Resource
    private UserSysService userSysService;

    /**
     * 用户全查询
     */
    @RequestMapping("/loadAllUser")
    public DataGridView loadAllUser(UserSysVo userVo) {
        IPage<UserSys> page = new Page<>(userVo.getPage(), userVo.getLimit());
        QueryWrapper<UserSys> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(userVo.getUserName()), "user_name", userVo.getUserName());
        queryWrapper.eq(StringUtils.isNotBlank(userVo.getAddress()), "address", userVo.getAddress());
        queryWrapper.eq("type", Constast.USER_TYPE_NORMAL);
        this.userSysService.page(page, queryWrapper);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    /**
     * 把用户名转成拼音
     */
    @RequestMapping("/changeChineseToPinyin")
    public Map<String, Object> changeChineseToPinyin(String username) {
        Map<String, Object> map = new HashMap<>();
        if (null != username) {
            map.put("value", PinyinUtils.getPingYin(username));
        } else {
            map.put("value", "");
        }
        return map;
    }

    /**
     * 添加用户
     */
    @RequestMapping("/addUser")
    public ResultObj addUser(UserSysVo userVo) {
        try {
            userVo.setStatus(0);
            userVo.setRank(0);
            userVo.setCardFlag(0);
            userVo.setInvest(0);
            userVo.setBalance(0);
            userVo.setPoints(0);
            userVo.setCardFlag(0);
            userVo.setCardFlag(0);
            userVo.setCardFlag(0);
            this.userSysService.save(userVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }


    /**
     * 根据用户ID查询一个用户
     */
    @RequestMapping("/loadUserById")
    public DataGridView loadUserById(Integer id) {
        return new DataGridView(this.userSysService.getById(id));
    }

    /**
     * 修改用户
     */
    @RequestMapping("/updateUser")
    public ResultObj updateUser(UserSysVo userVo) {
        try {
            this.userSysService.updateById(userVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除用户
     */
    @RequestMapping("/deleteUser")
    public ResultObj deleteUser(Integer id) {
        try {
            this.userSysService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
}

